package ntd.util.frame.haikang.remote;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import ntd.util.config.common.exception.ComException;
import com.c.Constant;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class RemoteHaiKang {

    private final static String ARTEMIS_PATH = "/artemis";
    private HaiKangConfig haiKangConfig;

    public static RemoteHaiKang me(HaiKangConfig haiKangConfig) {
        RemoteHaiKang remoteHaiKang = new RemoteHaiKang();
        ArtemisConfig.host = haiKangConfig.getHost();
        ArtemisConfig.appKey = haiKangConfig.getAppKey();
        ArtemisConfig.appSecret = haiKangConfig.getAppSecret();
        remoteHaiKang.haiKangConfig = haiKangConfig;
        return remoteHaiKang;
    }

    public String pageBmunicipal() {
        JSONObject params = initParams();
        return doPost("/api/bmunicipal/v1/artemis/cover/query/page", params.toString());
    }

    public String getStreamUrlByCameraIndexCode(String cameraIndexCode, String streamUrlProtocol) {
        JSONObject params = initParams();
        params.putOnce("cameraIndexCode", cameraIndexCode);
        params.putOnce("protocol", streamUrlProtocol);
        params.putOnce("streamType", 0);
        params.putOnce("expand", "streamform=rtp");
        return doPost("/api/video/v1/cameras/previewURLs", params.toString());
    }

    public String getOldStreamUrlByCameraIndexCode(String cameraIndexCode, String streamUrlProtocol, LocalDateTime beginTime, LocalDateTime endTime) {
        JSONObject params = initParams();
        params.putOnce("cameraIndexCode", cameraIndexCode);
        params.putOnce("protocol", streamUrlProtocol);
        params.putOnce("beginTime", DateUtil.format(beginTime, Constant.DATE_FORMAT_ISO8601));
        params.putOnce("endTime", DateUtil.format(endTime, Constant.DATE_FORMAT_ISO8601));
        params.putOnce("recordLocation", 1);
        params.putOnce("streamType", 0);
        params.putOnce("expand", "streamform=rtp");
        return doPost("/api/video/v1/cameras/playbackURLs", params.toString());
    }

    /**
     * 获取区域列表
     * @param parentIndexCode 父级区域编号
     * @return 返回数据 - json字符串
     */
    public String ListRegionByParentIndexCode(String parentIndexCode) {
        JSONObject params = initParams();
        params.putOnce("parentIndexCode", parentIndexCode);
        return doPost("/api/resource/v1/regions/subRegions", params.toString());
    }

    /**
     * 获取监控设备列表
     * @param regionIndexCode 区域编号
     * @return 返回数据 - json字符串
     */
    public String listCameraByRegionIndexCode(String regionIndexCode) {
        JSONObject params = initParams();
        params.putOnce("regionIndexCode", regionIndexCode);
        return doPost("/api/resource/v1/camera/advance/cameraList", params.toString());
    }

    public String doPost(String subPath, String params) {
        if (StrUtil.isNotBlank(params) && !JSONUtil.isTypeJSON(params)) {
            throw new ComException("海康请求参数[{}]异常", params);
        }
        return ArtemisHttpUtil.doPostStringArtemis(initPath(subPath), params, null, null, "application/json");
    }

    private static JSONObject initParams() {
        JSONObject params = new JSONObject();
        params.putOnce("pageNo", 1);
        params.putOnce("pageSize", 20);
        return params;
    }

    private Map<String, String> initPath(String subPath) {
        String protocol = haiKangConfig.getHttpsTag() ? Constant.HTTPS_PREFIX : Constant.HTTP_PREFIX;

        return new HashMap<String, String>(2) {{
            // 根据现场环境部署确认是http还是https
            put(protocol, ARTEMIS_PATH + subPath);
        }};
    }

    public static void main(String[] args) {
        HaiKangConfig haiKangConfig = new HaiKangConfig();
//        haiKangConfig.setHost("112.13.217.188");
//        haiKangConfig.setAppKey("20388978");
//        haiKangConfig.setAppSecret("Cmtk0eVmgPidaEEBhJ5T");
        haiKangConfig.setHost("117.158.195.123:801");
        haiKangConfig.setAppKey("23991440");
        haiKangConfig.setAppSecret("G9MvIH2NnYQ4hLJ1eG6p");
        haiKangConfig.setHttpsTag(true);
        RemoteHaiKang me = me(haiKangConfig);
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("pageNo", "1");
        jsonObject.putOnce("pageSize", "1");
        // 我们设备：0861428041703415
//        jsonObject.putOnce("deviceNumber", "0861428041703415");
        jsonObject.putOnce("deviceNumber", "0861428040913445");
//        jsonObject.putOnce("type", "2");
        // 告警数据查询
        me.doPost("/api/bmunicipal/v1/artemis/device/alarm/query/page", jsonObject.toString());
        // 采集数据查询
//        me.doPost("/api/bmunicipal/v1/artemis/device/data/query/page", jsonObject.toString());


    }
}
